package com.example.bmanliku;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CreateActivity extends AppCompatActivity {
    private Button Bt_submit;
    private EditText DetailedTag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        Bt_submit=findViewById(R.id.Bt_submit);
        DetailedTag=findViewById(R.id.DetailedTag);
    }

    private void submit() {
        String Tag = DetailedTag.getText().toString().trim();
        if (TextUtils.isEmpty(Tag)) {
            Toast.makeText(this, "案例不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //  validate success, do something
        InternetSever(Tag);
    }

    private void InternetSever(String Tag) {
        OkHttpClient client = new OkHttpClient();
        FormBody body = new FormBody.Builder()
                .add("casetext", Tag)
                .build();
        final Request request = new Request.Builder()
                .url("http://10.0.2.2:8000/file/")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback()
        {

            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(CreateActivity.this, "网络异常，请稍后再试！", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException
            {

                final String date=response.body().string();
                Gson gson=new Gson();
                Txt_cs txt_cs=gson.fromJson(date,Txt_cs.class);

                if (txt_cs.get_$StatusCode185()==200){
                    Toast.makeText(CreateActivity.this, txt_cs.getTag(), Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(CreateActivity.this, txt_cs.getCode(), Toast.LENGTH_SHORT).show();
                };

            }
        });

    }

    public void SubmitClick(View view) {
        submit();
    }
}